﻿ 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.Result;

namespace NTTDATA.BEENChina.ResultCertification.Util
{
    public class StrHelper
    {
        //  判断是否是数字 
        public static bool IsNumeric(string number)
        {
            try
            {

                for (int i = 0; i < number.Length; i++)
                {
                    if (!char.IsNumber(number, i))
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /*        if (c >= 0x4E00 && c <= 0x9FA5) MessageBox.Show(c + "是汉字");
                else if (c >= 0x3040 && c <= 0x309F) MessageBox.Show(c + "是平假名");
                else if (c >= 0x30A0 && c <= 0x30FF) MessageBox.Show(c + "是片假名");*/



        public static bool IsKana(string s)
        {
            bool bresult = false;
            try
            {

                foreach (char c in s)
                {
        
                    if (IsKana(c))
                    {
                        bresult = true;
                    }
                    else
                    {
                        bresult= false;
                    }


                    if (!bresult)
                        break;
                }

                return bresult;
            }
            catch
            {
                return false;
            }
        }

        public static List<char>GetKana(string s)
        {
            List<char> kanans = null;
            try
            {

                foreach (char c in s)
                {

                    if (IsKana(c))
                    {
                        if (kanans == null)
                            kanans = new List<char>();

                        kanans.Add(c);
                    }
                }
            }
            catch
            {
                return null;
            }

            return kanans;
        }


        public static List<char> GetKannji(string s)
        {
            List<char> kannjis = null;
            try
            {

                foreach (char c in s)
                {

                    if (IsKannji(c))
                    {
                        if (kannjis == null)
                            kannjis = new List<char>();

                        kannjis.Add(c);
                    }
                }
            }
            catch
            {
                return null;
            }

            return kannjis;
        }

        public static bool IsKannji(string s)
        {
            bool bresult = false;
            try
            {

                foreach (char c in s)
                {
                    if (IsKannji(c))
                    {
                        bresult = true;
                    }
                    else
                    {
                        bresult = false;
                    }


                    if (!bresult)
                        break;
                }

                return bresult;
            }
            catch
            {
                return false;
            }
        }

        public static bool  IsKannji(char c)
        {
            
            //CJK統合漢字 	    CJK Unified Ideographs 	4E00～9FFF
            //CJK統合漢字拡張A 	CJK Unified Ideographs Extension A 	3400～4DBF
            //CJK統合漢字拡張B 	CJK Unified Ideographs Extension B 	20000～2A6DF
            //CJK統合漢字拡張C 	CJK Unified Ideographs Extension C 	2A700～2B73F
            //CJK統合漢字拡張D 	CJK Unified Ideographs Extension D 	2B740～2B81F
            //CJK互換漢字       CJK Compatibility Ideographs 	F900～FAFF
            //CJK互換漢字補助 	CJK Compatibility Ideographs Supplement 	2F800～2FA1D
            //漢文用記号 	Kanbun 	3190～319F
            if ((c >= 0x4E00 && c <= 0x9FA5) ||
                (c >= 0x3400 && c <= 0x4DBF) ||
                (c >= 0x20000 && c <= 0x2A6DF) ||
                (c >= 0x2A700 && c <= 0x2B73F) ||
                (c >= 0x2B740 && c <= 0x2B81F) ||
                (c >= 0xF900 && c <= 0xFAFF) ||
                (c >= 0x2F800 && c <= 0x2FA1D)  
                )
            {
                return true;
            }
            return false;
        }

        public static bool IsKana(char c)
        {
 

            //平仮名 	Hiragana 	3040～309F 	JISを基にした平仮名です。
            //片仮名 	Katakana 	30A0～30FF 	JISを基にした片仮名です。
            if ((c >= 0x3040 && c <= 0x309F) || (c >= 0x30A0 && c <= 0x30FF))
            {
                return true;
            }
            return false;
        }

        public static bool IsExternalChar(char c)
        {
            if  (c >= 0xE000 && c <= 0xF8FF)  
            {
                return true;
            }
            return false;
        }



        public static string RankToString( EnumRank enumRank)
        {
            string result = string.Empty;
            switch (enumRank)
            {
                    case EnumRank.A:
                        result = "Aジョブ";
                        break;
                    case EnumRank.B:
                        result = "Bジョブ";
                        break;
                    case EnumRank.C:
                        result = "Cジョブ";
                        break;
                    case EnumRank.D:
                        result = "Cジョブ";
                        break;
                    case EnumRank.E:
                        result = "Eジョブ";
                        break;
                    case EnumRank.Final:
                        result = "納品結果";
                        break;

            }

            return result;
        }

        /// <summary>
        /// N个字符一换行
        /// </summary>
        /// <param name="resultString"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string StringAddNewLine(string resultString ,int length)
        {
            
            var temp = resultString.Split(new string[] { "\r\n","\n"}, StringSplitOptions.None);
            var result = new StringBuilder();



            foreach (string  item in temp)
            {
                int starIndex = 0;

                do
                {

                    string tempVal;

                    if (starIndex + length <= item.Length)
                    {
                        tempVal = item.Substring( starIndex, length);
                    }
                    else
                    {
                        tempVal = item.Substring(starIndex);
                    }

                    result.Append(tempVal);
                    result.Append(Environment.NewLine);

                    starIndex += length ;

                } while ( starIndex < item.Length);

            }

            return result.ToString();
        }

        public void test(string colResult, string dbName)
        {
            int starIndex = 0;
            int keyIndex = -1;
            do
            {

                keyIndex = colResult.IndexOfAny(dbName.ToCharArray(), starIndex);

                if (keyIndex.Equals(-1))
                    break;


                string tempVal = string.Empty;

                if (keyIndex + dbName.Length <= colResult.Length)
                {
                    tempVal = colResult.Substring(keyIndex, dbName.Length);
                }
                else
                {
                    tempVal = colResult.Substring(keyIndex);
                }

                Console.WriteLine(keyIndex  + "/" + tempVal);
              
                starIndex = keyIndex + 1;

            } while (keyIndex < 0 || starIndex < colResult.Length);
        }
    }
}
